const { $Toast } = require('../../../utils/iview/base/index');
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';

const app = getApp();


Page({
  data: {
    auth: true,
    isloade: { hidden: false },
    _baseUrl: app.globalData._baseUrl,
    _imgPath: app.globalData._imgPath,
    openid: app.globalData.openid,
    position: 'right',
    dail: '',
    img: []

  },
  onLoad() {
    var _that = this
    _that.setTitle('意见反馈');
    var { openid } = wx.getStorageSync('user');

    _that.setData({
      openid: openid,
      // id: wx.getStorageSync('id')
    })
  },



  close(e) {
    var _that = this;
    var img = _that.data.img;
    img.splice(e.currentTarget.dataset, 1)
    _that.setData({
      img: img
    })
  },

  //提交图片
  add_img: function () {
    var that = this,
      img = that.data.img;

    // if (img.length < 3) {
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        var tempFilesSize = res.tempFiles[0].size;
        if (tempFilesSize < 2000000) {

          var tempFilePaths = res.tempFilePaths[0]


          // that.data.img.push(tempFilePaths);
          that.data.img.push(tempFilePaths);

          that.setData({
            img: that.data.img
          })
          // console.log(tempFilePaths, that.data.img);

        } else {
          wx.showToast({
            title: '上传图片不能大于2M!',
            icon: 'none'
          })
        }


      }
    })
  },


  bindChanges(e) {
    console.log(e, "12")
    var value = e.detail.value;
    this.setData({
      dail: value
    })
    console.log(this.data.dail, "dail")
  },

  formSubmit(e) {
    let _that = this;

    var isclick = _that.data.isclick;
    if (isclick) {
      return;
    }

    _that.setData({
      isclick: 1
    })
    setTimeout(() => {
      _that.setData({
        isclick: 0
      })
    }, 3000)


    let values = e.detail.value;
    
    var token = wx.getStorageSync('token');
    console.log(token,"显示token");
    values.token = token;
    // values.store_id = wx.getStorageSync('id');
    values.comment = _that.data.dail
    setTimeout(() => {
      for (let i in values) {
        var value = values[i];

        console.log(i, "ii")
        console.log(token,"token12")
        // console.log(_that.data.store_id, "id")

        if (!value) {
          $Toast({
            content: '内容不能为空',
            type: 'warning'
          });
          return false;
        }

      }


      let net = new NetWork(this.data._baseUrl);
      net._request({
        url: 'api/v1/member/comment',
        data: values,
        method: 'POST'
      }, '', res => {
        let _data = res.data.data;

        $Toast({
          content: '反馈成功!',
          type: 'success'
        });
        setTimeout(() => {
          wx.navigateBack({
            delta: 2,
          });
        }, 2000)

      });
    }, 100)

  },

  uploadimg: function (data) {
    var that = this,
      lens = that.data.lens,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    console.log(data.url);
    wx.uploadFile({
      url: data.url,
      filePath: data.image[i],
      name: 'images',
      formData: data.formData,
      header: {
        "Content-Type": "multipart/form-data",
        //"sessionId": getApp().globalData.sessionId,
      },
      success: (resp) => {
        success++;
      },
      fail: (res) => {
        fail++;
      },
      complete: () => {
        i++;
        if (i == data.image.length) {   //当图片传完时，停止调用
          lens++;
          that.setData({
            lens: lens
          })
          $Toast({
            content: '反馈成功!',
            type: 'success'
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 2,
            });
          }, 2000)
          that.setData({
            tempFilePaths: []
          })
        } else {//若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },


  handleAnimalChange({ detail = {} }) {
    this.setData({
      checked: detail.current
    });
  },


  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    var that = this

    _that.setData({
      isclick: 0
    })

    that.isUserInfo();

  },


  /* 拨打电话 */
  telPhone() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: ''
    }, res => {

    });
  },
  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },
  setType(e) {
    var index = e.target.dataset.index;
    this.setData({
      roomType: index
    })
  },
  onShareAppMessage(str) {
    return {
      title: str,
      path: this.route
    }
  },
  /* 隐藏分享功能 */
  hideShare() {
    wx.hideShareMenu()
  },
  /* 分享功能 */
  showShare() {
    wx.showShareMenu()
  },
  /* 下拉刷新触发功能 */
  onPullDownRefresh() {
    // 下拉刷新事件
    // wx.startPullDownRefresh
    // wx.stopPullDownRefresh
    setTimeout(() => {
      wx.stopPullDownRefresh()
      console.log('触发下拉事件');
    }, 1000)

  },

  /* 开启上拉到底部触发功能 */
  startDonwEven() {
    wx.startPullDownRefresh()
  },
  /* 停止上拉到底部触发功能 */
  stopDownEven() {
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    // 上拉触底事件
    //console.log('触发触底事件')
  },

  onPageScroll(res) {
    // 页面滚动事件
    //console.log(res.scrollTop)
  },

  isUserInfo() {
    var _that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          console.log("1");
          _that.setData({
            auth: false
          })

        }
      }
    })
  },

  userInfo(e) {

    let _that = this;
    let _userInfo = e.detail.userInfo;
    let net = new NetWork(app.globalData._baseUrl);
    let _data = e.detail;


    if (_userInfo) {
      this.setData({
        auth: true
      })
    }

    wx.login({
      success(res) {
        if (res.code) {
          net._request({
            url: '/api/login/sqlogin',
            method: 'POST',
            data: {
              code: res.code,
              rawData: _data.rawData,
              signature: _data.signature,
              iv: _data.iv,
              encryptedData: _data.encryptedData
            },
          }, false, sres => {

            wx.setStorageSync('user', sres.data.data[0].user[0])

            _that.setData({
              openid: sres.data.data[0].user[0].openid,
              user: sres.data.data[0].user[0]
            })
            setTimeout(() => {
              if (sres.data.data[0].user[0].user_type != 1) {
                wx.navigateBack({ changed: true });
              }
            }, 500);

          }, res => {
            console.log("失败返回", res);
          });
        }
      }
    })
  },


})
